﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Collections;
using System.Data;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.Pages
{
    public partial class TestWebUserEditForm : BSCMFF.Core.MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        
        protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl =
                    (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

            //if (e.Item.DataItem.GetType() == typeof(Role))
            //{

            int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Role_ID"].ToString());
                BSCEntities context = new BSCEntities();
                Role r = (from role in context.Roles
                         where role.Role_ID == id
                         select role).Single();
                
                r.Name = (userControl.FindControl("Role") as RadTextBox).Text;
                
                context.SaveChanges();
           // }
            /*
            List<Role> categories = ListOfCategories;
            //Prepare new row to add it in the DataSource 
            string Id = editedItem.OwnerTableView
                            .DataKeyValues[editedItem.ItemIndex]["CategoryID"].ToString();
            IEnumerable<Role> changedCategories =
                            categories.Where(c => c.Role_ID == int.Parse(Id));

            if (changedCategories.Count() != 1)
            {
                RadGrid1.Controls
                    .Add(new LiteralControl("Unable to locate the category for updating."));
                e.Canceled = true;
                return;
            } 

            //Update new values
            Hashtable newValues = new Hashtable();

            newValues["Name"] = (userControl.FindControl("Role") as TextBox).Text;
            

            changedRows[0].BeginEdit();
            try
            {
                foreach (DictionaryEntry entry in newValues)
                {
                    changedRows[0][(string)entry.Key] = entry.Value;
                }
                changedRows[0].EndEdit();
                this.Employees.AcceptChanges();
            }
            catch (Exception ex)
            {
                changedRows[0].CancelEdit();

                Label lblError = new Label();
                lblError.Text = "Unable to update Employees. Reason: " + ex.Message;
                lblError.ForeColor = System.Drawing.Color.Red;
                RadGrid1.Controls.Add(lblError);

                e.Canceled = true;
            }*/
        }

        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl =
                    (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

            BSCEntities context = new BSCEntities();
            Role newRole = new Role();
            newRole.Name = (userControl.FindControl("Role") as RadTextBox).Text;

            context.Roles.AddObject(newRole);
            context.SaveChanges();
        }

        //protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        //{
        //    RadGrid1.DataSourceID
        //}
    }
}